<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/13
 * Time: 12:54
 */

namespace App\Http\Controllers\Account;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\AdminVail;
use App\Model\Account\AccountPrice;
use App\Model\Account\Administration;
use App\Tool\Error;
use Illuminate\Http\Request;
use Validator;


class AdministrationController extends Controller
{
    /**中介管理账号列表
     * @param Request $request
     */
    public function listAdmin(Request $request)
    {
        $params = $request->all();
        $where = [];
        $map = [];
        //公司名字
        if ($request->input('anm_branch_name')){
            $where[] = ['anm_branch_name', 'like', "%".$params['anm_branch_name']."%"];
        }
        //购买时间开始时间
        if ($request->input('start_time')){
            $where[] = ['anm_buytime', '>=', strtotime($params['start_time'])];
            $map[]   = ['nm_create_time', '>=', strtotime($params['start_time'])];
        }
        //购买时间结束时间
        if ($request->input('end_time')){
            $where[] = ['anm_buytime', '<=', strtotime($params['end_time'])];
            $map[]   = ['nm_create_time', '<=', strtotime($params['end_time'])];
        }
        //平台管理账号列表
        $list = Administration::adminList(env('PAGE_LIMIT_ADMIN'), $where);
        //统计当前公司的数量
        $count = Administration::adminCount($where);
        // 统计钱数
        $allmoney = Administration::allmoney($map);
        if(!$list) {
            return $this->error(Error::NULL_DATA,array('count' => $count, 'allmoney' => $allmoney));
        }
        return $this->success('ok', array('list' => $list, 'count' => $count, 'allmoney' => $allmoney));
    }

    //查中介缴费记录的列表  根据中介公司的ID
    public function getPayList(Request $request)
    {
        $id = $request->input('anm_id');
        $list = Administration::payList($id, env('PAGE_LIMIT_ADMIN'));
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

    //添加中介管理账号
    public function addAdmin(Request $request)
    {
        $params = $request->all();
        $vali = AdminVail::add($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Administration::adminAdd($params);
        if (!$status) {
            return $this->error(Error::ADD_ERROR);
        }
        return $this->success('ok', '添加成功');
    }

    /**审核通过   禁用启用中介公司所有的账号
     * @param Request $request
     */
    public function updateCompanyStatus(Request $request)
    {
        $cid = $request->input('c_id');//中介公司的ID
        $status = $request->input('status');//中介公司账号目前的状态值
        $status =  Administration::updateCompanyStatus($cid, $status);
        if ($status === -1) {
            return $this->error(Error::EDIT_ERROR);
        }
        if (!$status) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok', '操作成功');

    }

    //给中介公司续费
    public function pay(Request $request)
    {
        $params = $request->all();
        $vali = AdminVail::pay($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $status = Administration::renewTheAgency($params);
        if (!$status){
            return $this->error(Error::PAY_ERROR);
        }
        return $this->success('ok', '续费提交成功，等待审核');
    }



    //审核中介公司的续费
    public function examineRenew(Request $request)
    {
        $params = $request->all();
        $vali = AdminVail::renew($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = Administration::userRenew($params);
        if (!$res) {
            return $this->error(Error::EDIT_PAY_ERROR);
        }
        return $this->success('ok', '审核成功');
    }


    //会员等级列表
    public function memberList()
    {
        $member = new  AccountPrice();
        $list = $member->memberList();
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

    /**
     * 审核中介公司的账号
     */
    public function examineAccount(Request $request)
    {
        $params = $request->all();
        $vali = AdminVail::renew($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = Administration::examineAccount($params);
        if (!$res) {
            return $this->error(Error::EXAMINE_FAIL);
        }
        return $this->success('ok', '审核通过！');
    }


}